package com.fifth.user.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fifth.common.pojo.dto.CommonResult;
import com.fifth.common.pojo.entity.Comment;
import com.fifth.common.pojo.entity.Order;
import com.fifth.user.feign.OrderFeign;
import com.fifth.user.mapper.CommentMapper;
import com.fifth.user.service.CommentService;
import com.fifth.user.util.WebUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@Service
public class CommentServiceImpl implements CommentService {
    @Autowired
    private CommentMapper commentMapper;
    @Autowired
    private StringRedisTemplate redisTemplate;
    @Autowired
    private OrderFeign orderFeign;

    //根据商品查询评论信息
    @Override
    public CommonResult findComment(String goodsid, Integer pageCurrent, Integer pageSize) {
        IPage<Comment> page = new Page<>(pageCurrent, pageSize);
        QueryWrapper<Comment> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("u_comment.goodsid", goodsid);
        IPage<Comment> comment = commentMapper.selectCommentByGoodsid(page, queryWrapper);
        List<Comment> records = comment.getRecords();
        if (records.size() != 0) {
            for (Comment comment1 : records) {
                String img = comment1.getImg();
                if (img != null) {
                    String[] split = img.split(",");
                    comment1.setImgs(split);
                }
            }
            return new CommonResult(2000, "查询成功", comment);
        }
        return new CommonResult(5000, "暂时没有评论信息", null);
    }

    //根据id号删除评论信息
    @Override
    public CommonResult deleteById(String id) {
        QueryWrapper<Comment> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", id);
        int delete = commentMapper.delete(queryWrapper);
        if (delete != 0) {
            return new CommonResult(2000, "删除成功", null);
        }
        return new CommonResult(5000, "删除失败", null);
    }

    @Override
    public CommonResult updateComment(Comment comment) {
        if (comment.getInfo()==null|| comment.getInfo().equals("")){
            return new CommonResult(5001,"必须评论才能提交",null);
        }
        QueryWrapper<Comment> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", comment.getId());
        StringBuffer sb = new StringBuffer();
        String[] imgs = comment.getImgs();
        for (int i = 0; i < imgs.length; i++) {
            if (i != imgs.length - 1) {
                sb.append(imgs[i] + ",");
            } else {
                sb.append(imgs[i]);
            }
        }
        comment.setImg(sb.toString());
        int update = commentMapper.update(comment, queryWrapper);
        if (update == 1) {
            return new CommonResult(2000, "修改成功", null);
        } else {
            return new CommonResult(5000, "修改失败", null);
        }
    }

    @Override
    public CommonResult findByUserId() {
        HttpServletRequest request = WebUtils.getRequest();
        //获取token请求头
        String token = request.getHeader("token");
        //获取登陆者信息
        Map map = JSON.parseObject(redisTemplate.opsForValue().get(token), Map.class);
        Map user = (Map) map.get("user");
        String userid = (String) user.get("id");
        //List<Order> comments = orderFeign.addComment(userid);
        return new CommonResult(2000, "查询成功", null);
    }

    @Override
    public void addComment(String code, String goodsid, String userid) {
        Comment comment = new Comment();
        comment.setUserid(userid);
        comment.setCode(code);
        comment.setGoodsid(goodsid);
        int insert = commentMapper.insert(comment);
    }

    @Override
    public CommonResult findByGoodsId(String code, String goodsid) {
        QueryWrapper<Comment> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("code", code);
        queryWrapper.eq("goodsid", goodsid);
        queryWrapper.eq("is_deleted", 0);
        queryWrapper.eq("status", 0);
        List<Comment> comments = commentMapper.selectList(queryWrapper);
        if (comments.size() != 0) {
            return new CommonResult(2000, "查询成功", comments);
        }
        return new CommonResult(5000, "该订单商品尚未提交评论", null);
    }

    @Override
    public CommonResult findMyComment(String goodsid, Integer pageCurrent, Integer pageSize) {
        HttpServletRequest request = WebUtils.getRequest();
        //获取token请求头
        String token = request.getHeader("token");
        //获取登陆者信息
        Map map = JSON.parseObject(redisTemplate.opsForValue().get(token), Map.class);
        Map user = (Map) map.get("user");
        String userid = (String) user.get("id");
        IPage<Comment> page = new Page<>(pageCurrent, pageSize);
        QueryWrapper<Comment> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("u_comment.goodsid", goodsid);
        queryWrapper.eq("u_comment.userid", userid);
        IPage<Comment> comment = commentMapper.selectMyCommentByGoodsid(page, queryWrapper);
        List<Comment> records = comment.getRecords();
        if (records.size() != 0) {
            for (Comment comment1 : records) {
                String img = comment1.getImg();
                if (img != null) {
                    String[] split = img.split(",");
                    comment1.setImgs(split);
                }
            }
            return new CommonResult(2000, "查询成功", comment);
        }
        return new CommonResult(5000, "暂时没有评论信息", null);
    }


    @Override
    public CommonResult getCommentById(String id) {
        Comment comment = commentMapper.selectById(id);
        if (comment.getImg() != null && !comment.getImg().equals("")) {
            String[] split = comment.getImg().split(",");
            comment.setImgs(split);
        } else {
            String[] strings = new String[]{"", "", ""};
            comment.setImgs(strings);
        }
        return new CommonResult(2000, "查找成功", comment);
    }

    /**
     * 添加评论
     * @param comment
     * @return
     */
    @Override
    public CommonResult addCommentByUid(Comment comment) {
        //获取到requset
        HttpServletRequest request = WebUtils.getRequest();
        //获取token
        String token = request.getHeader("token");
        //根据token获取userid和手机号
        Map userMap = JSON.parseObject(redisTemplate.opsForValue().get(token), Map.class);
        String userid;
        if (userMap != null && userMap.containsKey("user")) {
            JSONObject user = (JSONObject) userMap.get("user");
            userid = user.getString("id");
        } else {
            return new CommonResult(4000, "暂未登录,请登录后再购买", null);
        }
        String[] imgs = comment.getImgs();
        comment.setImg(imgs[0]+","+imgs[1]+","+imgs[2]);
        comment.setUserid(userid);
        int insert = commentMapper.insert(comment);
        if (insert==1){
            //修改订单中该用户关于该商品的评论状态为1
            Integer integer = orderFeign.updateOrderCommentStatus(comment.getGoodsid(), userid);
            if (integer!=0){
                return new CommonResult(2000,"添加成功",null);
            }
        }
        return new CommonResult(5000,"添加失败",null);
    }
}
